package com.example.dotavideo.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.dotavideo.R;

public class ChildListAdapter extends BaseAdapter{
  
  Context context;
  private ArrayList<String> list;
  private Handler handler;
  
  public ChildListAdapter(Context context,ArrayList<String> list,Handler handler) {
    this.context = context;
    this.list = list;
    this.handler = handler;
  }
  
  @Override
  public int getCount() {
    return list != null ? list.size() : 0;
  }

  @Override
  public Object getItem(int arg0) {
    return null;
  }

  @Override
  public long getItemId(int arg0) {
    return 0;
  }

  @Override
  public View getView(int position, View view, ViewGroup group) {
    
    Holder holder;
    // 得到View
    if (view == null) {
      holder = new Holder();
      LayoutInflater inflater = LayoutInflater.from(context);
      view = inflater.inflate(R.layout.child_list_row, null);
      holder.tvTitle = (TextView) view.findViewById(R.id.row_title);

      view.setTag(holder);
    } else {
      holder = (Holder) view.getTag();
    }
    
    final String name = list.get(position).trim();
    holder.tvTitle.setText(name);
    holder.tvTitle.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        Log.e("------------", "onclick");
        Message msg = new Message();
        msg.what = 0;
        Bundle data = new Bundle();
        data.putString("key", name);
        msg.setData(data);
        handler.sendMessage(msg);
        
      }
    });
    
    return view;
  }
  class Holder {
    public TextView tvTitle;
  }

}
